package HeSuanServer.service.Impl;

import HeSuanServer.POJO.R.Result;
import HeSuanServer.POJO.R.StatusCode;
import HeSuanServer.mapper.IndexMapper;
import HeSuanServer.service.IndexService;
import cn.hutool.core.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @描述:
 * @author: LH
 * @date: 2023/4/9
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexMapper indexMapper;
    @Override
    public Result getY() {
        try {
            DateTime dateTime=new DateTime();
            dateTime.setHours(0);
            dateTime.setSeconds(0);
            dateTime.setMinutes(0);
            Map<String,Object> map=new HashMap<>();
            map.put("start",dateTime);
            DateTime end=new DateTime();
            end.setMinutes(59);
            end.setHours(23);
            end.setSeconds(59);
            map.put("end",end);
            Integer count=indexMapper.getY(map);
            return Result.Builder().Data(count).StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT_COUNT);
        }
    }

    @Override
    public Result totalY() {
        try {
            int count=indexMapper.totalY();
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT_COUNT);
        }
    }

    @Override
    public Result org() {
        try{
            int count=indexMapper.org();
            return Result.Builder().Data(count).StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT_COUNT);
        }
    }

    @Override
    public Result user() {
        try {
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(indexMapper.user());
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT_COUNT);
        }
    }

    @Override
    public Result tj() {
        try {
            Map<String,Object> map=new HashMap<>();
//        获取前七天的日期
            List<String> qrdate=new ArrayList<>();
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 6; i >= 0; i--) {
                String str = dateFormat.format(new DateTime().getTime()- 1000L *60*60*24*i);
                qrdate.add(str);
            }
            map.put("qrdate",qrdate);
//        获取前七天数据
            List<Integer> qrnumber= new ArrayList<>();
            for (int i = 6; i >= 0; i--) {
                Integer y = indexMapper.getY(getTime(i));
                qrnumber.add(y);
            }
            map.put("qrnumber",qrnumber);
//        获取本月前五机构的名称（由多到少）
            Map<String,Object> date=new HashMap<>();
            Calendar befdate=Calendar.getInstance();
            befdate.set(Calendar.DAY_OF_MONTH,1);
            befdate.set(Calendar.SECOND,0);
            befdate.set(Calendar.HOUR,0);
            befdate.set(Calendar.MINUTE,0);
            date.put("start",new DateTime(befdate));
            Calendar aftdate=Calendar.getInstance();
            aftdate.add(Calendar.MONTH,1);
            aftdate.set(Calendar.DAY_OF_MONTH,1);
            aftdate.set(Calendar.SECOND,0);
            aftdate.set(Calendar.HOUR,0);
            aftdate.set(Calendar.MINUTE,0);
            date.put("end",new DateTime(aftdate));
            List<Map<String,String>> r=indexMapper.orgGetY(date);
            List<Object> orgnumber= new ArrayList<>();
            List<Object> orgname=new ArrayList<>();
            for (Map<String, String> org : r) {
                orgnumber.add(org.get("count(*)"));
                orgname.add(org.get("orgName"));
            }
            map.put("orgnumber",orgnumber);
            map.put("orgname",orgname);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }
    public Map<String,Object> getTime(int i){
        DateTime date=new DateTime(new DateTime().getTime()- 1000L *60*60*24*i);
        date.setSeconds(0);
        date.setHours(0);
        date.setMinutes(0);
        Map<String,Object> map=new HashMap<>();
        map.put("start",date);
        DateTime end=new DateTime(new DateTime().getTime()- 1000L *60*60*24*i);
        end.setSeconds(59);
        end.setHours(23);
        end.setMinutes(59);
        map.put("end",end);
        return map;
    }
}
